#include<stdio.h>
#define MAX_stu 20
//学生最多20个
 
//单个学生
typedef struct 
{
    char name[128];//姓名
    char sex[3];   //性别
    int id;        //学号
    int age;       //年龄
    int sco;       //成绩
}stu_data;
 
//整个班级
typedef struct
{
    stu_data data[MAX_stu];
    int len;
}stu_class;
 
//按照成绩冒泡排序
void sort(stu_class * class)
{
    stu_data temp;
    for (size_t i = 0; i < class->len-1; i++)
    {
        for (size_t j = 1; j < class->len-i; j++)
        {
            if(class->data[j].sco<class->data[j-1].sco)
             {
                temp = class->data[j];
                class->data[j]=class->data[j-1];
                class->data[j-1]=temp;
             }
        }
        
    }

    for (size_t i = 0; i < class->len; i++)
    {
        printf("%s %s %d %d %d\n",class->data[i].name,class->data[i].sex,class->data[i].id,class->data[i].age,class->data[i].sco);
    }
    return ;    
}
 
//按照成绩查找
void find(stu_class * class,int target_sco)
{
  sort(class);//排序
 
  int left,right,mid;
 
  left=0;
  right=class->len-1;
  
  while(left<=right)
  {
    mid=(left+right)/2;
    if(class->data[mid].sco==target_sco)//找到
    {
        printf("恭喜查找成功\n");
        return ;
    }
    else if(class->data[mid].sco<target_sco)//大于中间值
    {
       left=mid+1;
    }
    else//小于中间值
    {
       right=mid-1;
    }
  }
 
  if(left>right)printf("查找失败\n");//没找到
 
  return ;
}
 
int main(int argc, char const *argv[])
{
    stu_class stu={
    .data={   
         {"张三","男",1,18,67},
         {"李四","男",2,18,98},
         {"王五","男",3,18,79},
         {"张六","男",4,18,100},
         {"李七","男",5,18,80}
    },
    .len=5
                 };
 
find(&stu,80);
 
return 0;
 
}